package net.molchat.android.scrn;


import net.molchat.android.core.Man;
import android.util.Log;


//FIXME Commented out class


//import java.io.IOException;
//
//import javax.microedition.lcdui.Image;
//
//import net.molchat.android.core.Man;
//import net.molchat.android.core.Mess;
//import net.molchat.android.core.Setting;
//import net.molchat.android.scrn.btn.Button;


/**
 * Main superclass for all graphical windows.
 * 
 * @author Valentine
 * 
 */
public abstract class WinGraph implements Win {

//==================================================================== Constants
public static final byte DM_NORMAL = 0; // Normal drag mode, messages moved
public static final byte DM_BUTTON = 1; // Drag started from button


//======================================================================= fields
// Touch screen coordinates
private int tOffBeginX; // Touch offset begin coordinate X
private int tOffBeginY; // Touch offset begin coordinate Y
private int tOffX; // Touch offset current coordinate X
private int tOffY; // Touch offset current coordinate Y
private int tX; // Touch pressed point X
private int tY; // Touch pressed point Y


//// Buttons set
//protected Button[] msgBtns;
//
//// Buttons box
//int boxX;
//int boxY;
//int boxW;
//int boxH;
//
//private Button btnPressed = null;
//
//
////private int path; // Touch drag path length, points X + points Y
protected int lastPX = -1;
protected int lastPY = -1;
//protected boolean fBtnShow = false;
private byte dragMode; // Current dragging mode


////================================================================== Constructor
//public WinGraph() {
//
//	try {
//		this.msgBtns = new Button[1];
//
//		msgBtns[0] = new Button();
//		msgBtns[0].style.icon = Image.createImage("/res/icon/menu-24px.png");
//		msgBtns[0].com = ScrMan.COM_MENU;
//	} catch (IOException e) {
//		Log.e("WinGraph.init", e);
//	}
//}


//========================================================== Пересчитать размеры
public void updSize() {

	// FIXME commented out block

	//	msgBtns[0].w = Draw.btnW;
	//	msgBtns[0].h = Draw.btnH;
}


////======================================================================= addBtn
///**
// * Adds button to buttons set.
// * 
// */
//public void addBtn(Button btn) {
//
//	if (this.msgBtns == null) {
//		this.msgBtns = new Button[Setting.MAX_BTNS_PER_WIN];
//	}
//	// TODO Start from here
//}


////======================================================================= getBtn
//private Button getBtn(int x, int y) {
//
//	if (Draw.isInside(x, y, this.boxX, this.boxY, this.boxW, this.boxH)) {
//		for (int q = this.msgBtns.length - 1; q >= 0; --q) {
//			if (Draw.isInside(x, y, this.msgBtns[q].x, this.msgBtns[q].y,
//					this.msgBtns[q].w, this.msgBtns[q].h)) {
//				this.dragMode = DM_BUTTON;
//				return this.msgBtns[q];
//			}
//		}
//	}
//
//	return null;
//}
//
//
////===================================================================== drawBtns
//public abstract void drawBtns();


//===================================================================== doSelect
protected abstract void doSelect(int x, int y);


//========================================================================= tick
@Override
public boolean tick() {

	boolean fFlush = false;

	// About to drag with touch screen
	int dragOffX = 0;
	int dragOffY = 0;

	if (this.tOffX != this.tOffBeginX || this.tOffY != this.tOffBeginY) {
		synchronized (this) {
			if (this.dragMode == DM_NORMAL) {
				dragOffX = this.tOffX - this.tOffBeginX;
				dragOffY = this.tOffY - this.tOffBeginY;

				this.tOffBeginX = this.tOffX;
				this.tOffBeginY = this.tOffY;
			}
		}

		Log.d("ScrMan.tick", "pointerDragged (" + dragOffX + ";" + dragOffY + ")");
		fFlush |= doDrag(dragOffX, dragOffY);
	}

	return fFlush;
}


//======================================================================= doDrag
protected abstract boolean doDrag(int dragOffX, int dragOffY);


//=============================================================== pointerPressed
@Override
public void pointerPressed(int x, int y) {

	// FIXME commented out method

	Log.d("Wingraph.pointerPressed", "Pointer pressed on " + x + ", " + y);

	dragMode = DM_NORMAL;
	//	btnPressed = null;
	//
	//	if (this.fBtnShow) {
	//		Button btn = getBtn(x, y);
	//		if (btn != null) {
	//			btnPressed = btn;
	//			dragMode = DM_BUTTON;
	//			btn.setState(Button.ST_PRESS);
	//			btn.draw(Draw.g);
	//		}
	//	}

	synchronized (this) {
		this.tX = this.tOffBeginX = this.tOffX = x;
		this.tY = this.tOffBeginY = this.tOffY = y;
	}
}


@Override
public void pointerDragged(int toX, int toY) {

	synchronized (this) {
		this.tOffX = toX;
		this.tOffY = toY;
	}
}


//============================================================== pointerReleased
@Override
public void pointerReleased(int x, int y) {

	// FIXME commented out method

	Log.v("ScrMan.pointerPressed", "pointerReleased (" + x + ";" + y + ")");
	int offsetX;
	int offsetY;

	this.lastPX = x;
	this.lastPY = y;

	synchronized (this) {
		this.tOffBeginX = this.tOffX = x;
		this.tOffBeginY = this.tOffY = y;

		offsetX = x - tX;
		offsetY = y - tY;
	}

	//	if (this.btnPressed != null) { // Button was pressed
	//		Button btnRel = getBtn(x, y);
	//		btnPressed.setState(Button.ST_NORMAL);
	//		btnPressed.draw(Draw.g);
	//		if (btnRel == btnPressed) {
	//			Log.v("WinGraph.pointerReleased", "Btn was pressed and released: "
	//					+ btnPressed);
	//			Man.scrMan.commandAction(Man.scrMan.coms[btnPressed.com], null);
	//		}
	//		btnPressed = null;
	//	} else {
	if ((Math.abs(offsetX) >> 1) > Math.abs(offsetY)
			&& (Math.abs(offsetX) > ((Draw.dispW > Draw.dispH ? Draw.dispH
					: Draw.dispW) >> 3))) { // Moved to left or to right
		if (offsetX > 0) {
			Man.scrMan.showPrevCont();
		} else {
			Man.scrMan.showNextCont();
		}
	} else if (Math.abs(offsetX) <= (Draw.dispW >> 4)
			&& Math.abs(offsetY) <= (Draw.dispH >> 4)) { // Just pressed
		doSelect(x, y);
		//		drawBtns();
		Man.scrMan.showWin(null);
	}
	//	}

}


}
